# -*- Python -*-
# ex: set syntax=python:

# This is a sample buildmaster config file. It must be installed as
# 'master.cfg' in your buildmaster's base directory.

import config
import master_site_config

from common import chromium_utils

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

####### CHANGESOURCES

# the 'change_source' setting tells the buildmaster how it should find out
# about source code changes.  Here we point to the buildbot clone of pyflakes.

# Note that pollinterval is in seconds!
# Polls the repo for new changes every 30 seconds
from buildbot.changes import svnpoller
poller = svnpoller.SVNPoller(svnurl='http://sfntly.googlecode.com/svn/trunk/',
                             svnbin=chromium_utils.SVN_BIN,
                             pollinterval=30)

c['change_source'] = [ poller ]

####### BUILDERS

# The 'builders' list defines the Builders, which tell Buildbot how to perform a
# build: what steps, and which slaves can execute them.  Note that any
# particular build will only take place on one slave.

from buildbot.process.factory import BuildFactory
from buildbot.steps.source import SVN

from buildbot.steps.shell import ShellCommand
from buildbot.steps.shell import Configure, Compile, Test

SFNTLY_CPP_ROOT="build/cpp"

### UNIX
clang_cmake_step = Configure(command=["cmake", ".."],
                             workdir=SFNTLY_CPP_ROOT + "/build",
                             haltOnFailure=True,
                             env={"CC":"clang", "CXX":"clang++"})
gcc_cmake_step = Configure(command=["cmake", ".."],
                           workdir=SFNTLY_CPP_ROOT + "/build",
                           haltOnFailure=True,
                           env={"CC":"gcc", "CXX":"g++"})

# Enable multi-core builds on Unix
# Linux VMs have 4 cores
linux_make_step = Compile(command=["make", "-j5"],
                          workdir=SFNTLY_CPP_ROOT + "/build")
# Mac Minis have 2 cores
mac_make_step = Compile(command=["make", "-j3"],
                          workdir=SFNTLY_CPP_ROOT + "/build")

unix_steps = [
    # check out the source
    SVN(svnurl="http://sfntly.googlecode.com/svn/trunk/"),

    # get google test and setup symlinks
    ShellCommand(command=["unzip", "-o",
                          "ext/redist/gtest-1.6.0.zip",
                          "-d", "ext/"],
                 workdir=SFNTLY_CPP_ROOT),
    ShellCommand(command=["ln", "-s", "-f",
                          "gtest-1.6.0", "gtest"],
                 workdir=SFNTLY_CPP_ROOT + "/ext"),
    ShellCommand(command=["ln", "-s", "-f",
                          "../../ext/gtest", "gtest"],
                 workdir=SFNTLY_CPP_ROOT + "/src/test"),

    # generate Makefiles
    ShellCommand(command=["rm", "-rf", "build"],
                 workdir=SFNTLY_CPP_ROOT),
    ShellCommand(command=["mkdir", "build"],
                 workdir=SFNTLY_CPP_ROOT),

    # will replace when instantiating, step 6
    None,
    # generate binaries (run make)
    # will replace when instantiating, step 7
    None,

    # copy test fonts to bin/ folder
    ShellCommand(command=["cp", "data/ext/Tuffy.ttf",
                          "build/bin/"], workdir=SFNTLY_CPP_ROOT),
    ShellCommand(command=["cp", "data/ext/AnonymousPro-Regular.ttf",
                              "build/bin/"], workdir=SFNTLY_CPP_ROOT),
    # run unit tests
    Test(command=["./subsetter", "Tuffy.ttf", "Tuffy-s.ttf"],
         workdir=SFNTLY_CPP_ROOT + "/build/bin"),
    Test(command=["./unit_test"],
         workdir=SFNTLY_CPP_ROOT + "/build/bin")
    ]

clang_linux_steps = unix_steps
clang_linux_steps[6] = clang_cmake_step
clang_linux_steps[7] = linux_make_step
clang_linux_factory = BuildFactory(clang_linux_steps)

gcc_linux_steps = unix_steps
gcc_linux_steps[6] = gcc_cmake_step
gcc_linux_steps[7] = linux_make_step
gcc_linux_factory = BuildFactory(gcc_linux_steps)

clang_mac_steps = unix_steps
clang_mac_steps[6] = clang_cmake_step
clang_mac_steps[7] = mac_make_step
clang_mac_factory = BuildFactory(clang_mac_steps)

gcc_mac_steps = unix_steps
gcc_mac_steps[6] = gcc_cmake_step
gcc_mac_steps[7] = mac_make_step
gcc_mac_factory = BuildFactory(gcc_mac_steps)

### Windows
SFNTLY_CPP_ROOT_WIN="build\\cpp"
windows_steps = [
    # check out the source
    SVN(svnurl="http://sfntly.googlecode.com/svn/trunk/"),

    # get google test and setup symlinks
    ShellCommand(command=["rm", "-rf", "gtest", "icu", "cmake"],
                 workdir=SFNTLY_CPP_ROOT_WIN + "\\ext"),
    ShellCommand(command=["mkdir", "icu"],
                 workdir=SFNTLY_CPP_ROOT_WIN + "\\ext"),
    ShellCommand(command=["unzip", "-o",
                          "redist/gtest-1.6.0.zip"],
                 workdir=SFNTLY_CPP_ROOT_WIN + "\\ext"),
    ShellCommand(command=["mv",
                          "gtest-1.6.0", "gtest"],
                 workdir=SFNTLY_CPP_ROOT_WIN + "\\ext"),
    ShellCommand(command=["unzip", "-o",
                          "redist/icu4c-4_6_1-Win32-msvc10",
                          "-d", "."],
                 workdir=SFNTLY_CPP_ROOT_WIN + "\\ext"),
    ShellCommand(command=["unzip", "-o",
                          "redist\\cmake-2.8.5-win32-x86.zip",
                          "-d", "."],
                 workdir=SFNTLY_CPP_ROOT_WIN + "\\ext"),
    ShellCommand(command=["mv",
                          "cmake-2.8.5-win32-x86", "cmake"],
                 workdir=SFNTLY_CPP_ROOT_WIN + "\\ext"),

    # generate Makefiles
    ShellCommand(command=["rm", "-rf", "build"],
                 workdir=SFNTLY_CPP_ROOT_WIN),
    ShellCommand(command=["mkdir", "build"],
                 workdir=SFNTLY_CPP_ROOT_WIN),
    Configure(command=["cmd", "/c", "..\\ext\\cmake\\bin\\cmake.exe", "..",
                       "-G", "Visual Studio 9 2008"],
              workdir=SFNTLY_CPP_ROOT_WIN + "\\build",
              haltOnFailure=True),

    # generate binaries
    Compile(command=["vcvars32.bat"], workdir=SFNTLY_CPP_ROOT_WIN + "\\build"),
    Compile(command=["devenv", "sfntly.sln", "/build"],
            workdir=SFNTLY_CPP_ROOT_WIN + "\\build"),

    # copy test fonts to bin/ folder
    ShellCommand(command=["cp", "data\\ext\\Tuffy.ttf",
                          "build\\bin\\Debug\\"], workdir=SFNTLY_CPP_ROOT_WIN),
    ShellCommand(command=["cp", "data\\ext\\AnonymousPro-Regular.ttf",
                          "build\\bin\\Debug\\"], workdir=SFNTLY_CPP_ROOT_WIN),
    ShellCommand(command=["cp", "ext\\icu\\bin\\icudt46.dll",
                          "build/bin/Debug"], workdir=SFNTLY_CPP_ROOT_WIN),
    ShellCommand(command=["cp", "ext\\icu\\bin\\icuuc46.dll",
                          "build/bin/Debug"], workdir=SFNTLY_CPP_ROOT_WIN),

    # run unit tests
    Test(command=["cmd", "/c", "subsetter.exe", "Tuffy.ttf", "Tuffy-s.ttf"],
         workdir=SFNTLY_CPP_ROOT_WIN + "\\build\\bin\\Debug"),
    Test(command=["cmd", "/c", "unit_test.exe"],
         workdir=SFNTLY_CPP_ROOT_WIN + "\\build\\bin\\Debug")
    ]

vc2008_win_factory = BuildFactory(windows_steps)

windows_steps[10] = Configure(command=["cmd", "/c",
                                       "..\\ext\\cmake\\bin\\cmake.exe", "..",
                                       "-G", "Visual Studio 10"],
                              workdir=SFNTLY_CPP_ROOT_WIN + "\\build",
                              haltOnFailure=True)
windows_steps[11] = Compile(command=["vsvars32.bat"],
                            workdir=SFNTLY_CPP_ROOT_WIN + "\\build")
windows_steps[12] = Compile(command=["msbuild", "sfntly.sln"],
                            workdir=SFNTLY_CPP_ROOT_WIN + "\\build")

vc2010_win_factory = BuildFactory(windows_steps)

### Java
SFNTLY_JAVA_ROOT="build/java"
java_steps = [
    # check out the source
    SVN(svnurl="http://sfntly.googlecode.com/svn/trunk/")
    ]

java_mac_factory = BuildFactory(java_steps)

from buildbot.config import BuilderConfig

b_sfntly_clang_linux = {
    'name' : 'Clang Linux',
    'builddir' : 'clang-linux',
    'factory' : clang_linux_factory,
    'auto_reboot' : False,
    }
b_sfntly_gcc_linux = {
    'name' : 'GCC Linux',
    'builddir' : 'gcc-linux',
    'factory' : gcc_linux_factory,
    'auto_reboot' : False,
    }
b_sfntly_clang_mac = {
    'name' : 'Clang Mac',
    'builddir' : 'clang-mac',
    'factory' : clang_mac_factory,
    'auto_reboot' : False,
    }
b_sfntly_gcc_mac = {
    'name' : 'GCC Mac',
    'builddir' : 'gcc-mac',
    'factory' : gcc_mac_factory,
    'auto_reboot' : False,
    }
b_sfntly_vc2008_win = {
    'name' : 'VC2008 Windows',
    'builddir' : 'vc2008-win',
    'factory' : vc2008_win_factory,
    'auto_reboot' : False,
    }
b_sfntly_vc2010_win = {
    'name' : 'VC2010 Windows',
    'builddir' : 'vc2010-win',
    'factory' : vc2010_win_factory,
    'auto_reboot' : False,
    }
b_sfntly_java_mac = {
    'name' : 'Java Mac',
    'builddir' : 'java',
    'factory' : java_mac_factory,
    'auto_reboot' : False,
    }

c['builders'] = [b_sfntly_clang_linux,
                 b_sfntly_gcc_linux,
                 b_sfntly_clang_mac,
                 b_sfntly_gcc_mac,
                 b_sfntly_vc2008_win,
                 b_sfntly_vc2010_win,
                 b_sfntly_java_mac]

from master import master_utils
from master import slaves_list

slaves = slaves_list.SlavesList('slaves.cfg', 'Sfntly')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])

####### SCHEDULERS

# Configure the Schedulers, which decide how to react to incoming changes.
# In this case, just kick off a 'runtests' build

# Run all tests 30 seconds after a change has been detected
from buildbot.schedulers.basic import SingleBranchScheduler
from buildbot.schedulers import timed

allBuilders = [builder['name'] for builder in c['builders']]
c['schedulers'] = [
    SingleBranchScheduler(
        name="all",
        branch=None,
        treeStableTimer=30,
        builderNames=allBuilders)
]

####### BUILDSLAVES

ActiveMaster = master_site_config.Sfntly

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)

ActiveMaster = master_site_config.Sfntly
c['projectName'] = ActiveMaster.project_name
c['projectURL'] = ActiveMaster.project_url
# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster)

####### DB URL

# This specifies what database buildbot uses to store change and scheduler
# state.  You can leave this at its default for all but the largest
# installations.
c['db_url'] = "sqlite:///state.sqlite"
